<template>
    <div>
        <el-dialog v-model="props.visible" title="设备/机台列表" width="60%" @close="handleCancel">
            <el-form :model="queryParams" ref="queryRef" :inline="true">
                <el-form-item label="设备编码" prop="machineNo">
                    <el-input v-model.trim="queryParams.machineNo" placeholder="请输入设备编码" @keyup.enter="handleQuery" />
                </el-form-item>
                <el-form-item label="设备名称" prop="machineName">
                    <el-input v-model.trim="queryParams.machineName" placeholder="请输入设备名称" @keyup.enter="handleQuery" />
                </el-form-item>
                <el-form-item>
                    <el-button type="primary" icon="Search" @click="handleQuery">搜索</el-button>
                    <el-button icon="Refresh" @click="resetQuery">重置</el-button>
                </el-form-item>
            </el-form>
            <el-button style="margin-bottom: 5px;" type="primary" plain @click="handleSubmit"
                :disabled="multiple">确认</el-button>
            <el-table v-loading="loading" :data="machineList" row-key="pid" stripe   height="65vh" border style="cursor: pointer;"
                @selection-change="handleSelectionChange">
                <el-table-column type="selection" :reserve-selection="true" width="55" />
                <el-table-column label="序号" width="60" type="index" align="center" />
                <el-table-column label="设备编码" min-width="160" align="center"  prop="machineNo">
                </el-table-column>
                <el-table-column label="设备名称" width="220" align="center" prop="machineName">
                </el-table-column>
                <el-table-column label="车间编码" align="center"  prop="workshopNo">
                </el-table-column>
                <el-table-column label="车间名称" align="center" prop="workshopName">
                </el-table-column>
                <el-table-column label="版材尺寸" align="center" prop="size" min-width="100">
                    <template v-slot="{ row }">
                        <dict-tag :options="ems_machine_info_size" :value="row.size" />
                    </template>
                </el-table-column>
                <el-table-column label="损耗比例(%)" align="center" prop="wasteRate" width="100" >
                    <template v-slot="{ row }">
                        <dict-tag :options="ems_machine_info_waste_rate" :value="row.wasteRate" />
                    </template>
                </el-table-column>
                <el-table-column label="机型" align="center" prop="model" width="100" >
                    <template v-slot="{ row }">
                        <dict-tag :options="ems_machine_info_model" :value="row.model" />
                    </template>
                </el-table-column>
                <el-table-column label="产能(令/时、吨/时)" align="center" min-width="100" prop="capacity"
                     />
                <el-table-column label="类型" align="center" prop="type" width="100">
                    <template v-slot="{ row }">
                        <dict-tag :options="ems_machine_info_type" :value="row.type" />
                    </template>
                </el-table-column>
                <el-table-column label="负责人" align="center" prop="headUserName" min-width="100"
                   />
                <el-table-column label="负责人电话" align="center" prop="headUserPhone" min-width="120"
                    />
            </el-table>
            <div class="footer">
                <pagination v-show="total > 0" :total="total" v-model:page="queryParams.pageNum"
                    v-model:limit="queryParams.pageSize" @pagination="getList" />
            </div>
        </el-dialog>
    </div>
</template>

<script setup>
const emit = defineEmits(['changeUpdate'])
import { getList as getDataList } from '@/api/basicInfo/workbench'

const { proxy } = getCurrentInstance();
const { ems_machine_info_size, ems_machine_info_waste_rate,
    ems_machine_info_model,
    ems_machine_info_type
} = proxy.useDict("ems_machine_info_size", "ems_machine_info_waste_rate", "ems_machine_info_model", "ems_machine_info_type"
);
const props = defineProps({
    visible: {
        type: Boolean,
        default: false
    },

})
watch(() => props.visible, (val) => {
    if (val) {
        handleQuery()
    }
})
const machineList = ref([])
const multiple = ref(true)
const selectRow = ref([])
const loading = ref(false)
const queryParams = ref({
    pageNum: 1,
    pageSize: 50,
    status: 2
})
const total = ref(0)
// 选中
const handleSelectionChange = (row) => {
    selectRow.value = row
    multiple.value = !row.length
}
const handleSubmit = () => {
    emit('changeUpdate', selectRow.value)
}
const handleCancel = () => {
    proxy.resetForm("queryRef");
    emit('cancel')
}
const getList = async () => {
    loading.value = true
    const res = await getDataList(queryParams.value)
    loading.value = false
    machineList.value = res.rows
    total.value = res.total
}
// 搜索
const handleQuery = () => {
    queryParams.value.pageNum = 1;
    getList();
}
/** 重置按钮操作 */
const resetQuery = () => {
    queryParams.value.machineNo = undefined
    queryParams.value.machineName = undefined
    proxy.resetForm("queryRef");
    handleQuery();
};

getList()
</script>

<style lang="scss" scoped></style>